package com.ymttest.database.mapping;

import com.shop2cn.iapi.agentproducts.parameter.*;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.iapi.productmanager.parameter.BrandPo;
import com.ymatou.iapi.productmanager.parameter.SellerBrand;
import com.ymttest.database.model.GrouponCatalogs;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.model.Ymt_ProductDescConfigWord;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;
//商品合库 新库
    public interface YmtProductsIMapper {
    List<YmtProductsI> selectProductByProductId(String sProductId);
    List<Map> selectProductByProductIdforMap(String sProductId);
    int updateValidtimeByProductId(Date dvalidStart ,Date dvalidEnd , String sProductId);
    int updateSellerLoginByUserId(Date dStartTime ,Date dEndTime , String userId);
    List<Map> selectSubscribeRecord(int iUserId ,String sSKU);
    int updateCatalogTypeByProductId(String productId,int catalogType);
    List<Map> getSellerProductDetailList(Map m);
    List<Map> getSellerProductDetailList_product(String productId);
    List<Map> getSellerProductDetailList_Catalogs(Map m);
    List<Map> getSellerProductDetailList_CatalogProperty(String productId);
    List<Map> getSellerProductDetailList_ProductPicture(String productId);
    List<CategoryDto> getSecondCategoryByUserId(int userId);
    int insertAgentProduct(AgentProductPo po);
    @Update("update ymt_AgentProducts set AutoMarkup=#{AutoMarkup} where  productId = #{productId}")
    int updateAgentProductAutoMarkup(Map m);
    
    @Delete("delete Ymt_Products where sProductId=#{productId}")
    int DeleteProductsByProductId(Map m);
    
    @Delete("delete Ymt_Catalogs where sProductId=#{productId}")
    int DeleteCataLogsByProductId(Map m);
    
    @Select("select iTotalStock from Ymt_Products  where  sProductId = #{productId}")
    Map getiTotalStock(String productId);
    @Update("update Ymt_Products set iaction=#{iaction} where  sProductId = #{productId}")
    int updateProductAction(String productId,Integer iaction);
    @Update("update Ymt_Products set PackageDeliveryType=#{packageDeliveryType} where sProductId = #{productId}")
    int updateProductPackageDeliveryType(@Param("productId") String productId,@Param("packageDeliveryType") int packageDeliveryType);
//    @Select("select iTotalStock from Ymt_Products  where  sProductId = #{productId}")
//    Map getiTotalStock(String productId);
    @Select("select * from Ymt_ProductDescConfigWord where ProductId = #{productId}")
    Ymt_ProductDescConfigWord getProductDescConfigWord(String productId);
    @Update("update Ymt_ProductPspInfo set PspStatus=#{PspStatus} where ProductId=#{ProductId}") 
    int updatePspStatus(String ProductId,int PspStatus);
    @Select("select count(1) as c from Ymt_Products where iUserId = ${sellerId} and iAction >= 0 "
    		+ "and validEnd > getdate() and sProductId != #{productId}")
    int getProductSeatUserNum(@Param("sellerId")int sellerId,@Param("productId")String productId);
    
    
    @Select("SELECT * FROM Ymt_Products WHERE iSpuId = #{SpuId} AND iUserId = #{sellerId}")
    List<Map> selectSpuProducts(@Param("sellerId")int sellerId,@Param("SpuId")int SpuId);
    
    @Select(" SELECT DISTINCT c.iCategoryId AS categoryId, c.sCategory AS categoryName, c.sCategoryEn AS categoryNameEn FROM Ymt_Products p LEFT JOIN [Ymt_ProductCategory] c ON p.iCategoryId = c.iCategoryId WHERE p.iAction != -1 and c.iAction != -1 and p.iUserId = #{userid}")
    List<Map> getCategorys(@Param("userid")int userid);
    @Select(" SELECT DISTINCT b.iBrandId, b.sBrand, b.sBrandEn FROM Ymt_Products p LEFT JOIN [Ymt_ProductBrand] B ON p.iBrandId = b.iBrandId  WHERE p.iAction != -1 and b.iAction != -1 and p.iUserId = #{userid}")
    List<Map> getBrands(@Param("userid")int userid);
    @Select("SELECT DISTINCT a.iCategoryId AS thirdCategoryId, a.sCategory AS thirdCategoryName, a.sCategoryEn AS ThirdCategoryNameEn,"+
        "c.iCategoryId AS SecondCategoryId, c.sCategory AS SecondCategoryName, c.sCategoryEn AS SecondCategorNameyEn "+
        "FROM Ymt_Products p "+
        "INNER JOIN Ymt_ProductCategory a ON p.iThirdCategoryId = a.iCategoryId AND a.iAction >- 1"+
       " INNER JOIN Ymt_ProductCategory c ON a.iMasterCategory = c.iCategoryId AND c.iAction >- 1"+
       " WHERE P.iUserId = #{userId} AND P.iAction >- 1")
    List<Map> getFullProductCategoriesByUserId(@Param("userid")int userid);
    @Select("SELECT B.iBrandId as brandId, B.sBrandName as brandName, B.dAddTime as addTime,"+
      "B.iReferenceType as referenceType, B.iCreateUserId as createUserId, B.iComBrandId as comBrandId"+
       " FROM [dbo].[Seller_UserBrand] A INNER JOIN [dbo].[Seller_Brands] B ON A.iBrandId = B.iBrandId"+
       " AND A.iUserId = B.iCreateUserId WHERE A.iUserId = #{userid} AND ISNULL(B.iComBrandId, 0) <> 0"+
       " ORDER BY A.iTotalCount DESC")
    List<SellerBrand> getSellerBrand(@Param("userid")int userid);
    List<BrandPo> getCommonBrands(@Param("action") int action, @Param("brandIds") List<Integer> brandIds);
    @Select("SELECT p.PspStatus as pspStatus From Ymt_ProductPspInfo p where p.ProductId = #{productId}")
    List<Map> getPspStatus(@Param("productId")String productId);
    @Select("SELECT p.iIsTop as itop From Ymt_ProductsInLive p where p.sProductId = #{productId} AND p.iUserId = #{sellerId}")
    List<Map> getProductsInLiveTop(String productId,Integer sellerId);
    @Update("UPDATE Ymt_ProductsInLive set iIsTop=#{itop} where sProductId = #{productId} AND iUserId = #{sellerId}")
    int updateProductsInLiveTop(String productId,int sellerId);
    @Select("SELECT e.ID as Id,e.SellerId as sellerId,e.TemplateName as tempName,e.Description as description,e.PicUrls as pic,e.TemplateType as type from Ymt_ProductDescExtra e where e.ID = #{Id}")
    List<Map> getProductTemplate(Integer id);
    @Select("SElECT p.IsPspProduct from Ymt_Products p where p.iUserId=#{sellerId}")
    List<Map> getBatchUpdatePspProductStatus(Integer sellerId);
    @Select("SElECT p.CheckStatus,p.PspStatus from Ymt_ProductPspInfo p where p.SellerId = #{sellerId} and p.PspStatus in (2,3,4)")
    List<Map> getBatchUpdatePspProductStatus2(Integer sellerId);
    @Select("select count(1) as c from Ymt_Products where iUserId = #{sellerId} and iAction >= 0 and validEnd > getdate() and sProductId != #{productId}")
    List<Map> getProductSeatNum(String productId,Integer sellerId);
    @Select("select * from Ymt_ProductPspInfo where SellerId = #{SellerId} and ProductId = #{ProductId}")
    List<Map> selectPspStatus(String productId,Integer sellerId);
    @Select("select * from Ymt_CommisionLogs where ProductId = #{productId}")
    List<Map> selectCommissionLogByProductId(String productId);
    @Select("select yc.sSKU from Ymt_Products(NOLOCK) yp join Ymt_Catalogs(NOLOCK) yc on yp.sProductId=yc.sProductId" +
            " where yp.iUserId = ${userId}" +
            " and yp.iCatalogType=2" +
            " and yc.iAction>-1" +
            " and yp.iaction >-1")
    List<String> GetFBXCatalogsByUserId(int userId);
    /**
     * @param map  GrouponProducts.ProductId=pid and GrouponProducts.Version=version and GrouponProducts.Status=status
     * @return
     */
    @Select("<script>select * from GrouponProducts "+
    		"<where>"+
    			 "<if test='pid !=null '>" + "ProductId=#{pid}	" + "</if>"+ 
    			"<if test='version !=null '>" + "and Version=#{version}	" + "</if>"+
    			"<if test='status !=null '>" + "and Status=#{status}	" + "</if>"+
    		"</where>"+"</script>")
    List<GrouponProducts> selectGrouponProducts(Map map);
    /**
     * @param map CatalogId=cid Version=version
     * @return
     */
    @Select("<script>select * from GrouponCatalogs "+
    		"<where>"+
    			 "<if test='cid !=null '>" + "CatalogId=#{cid}	" + "</if>"+ 
    			"<if test='version !=null '>" + "and Version=#{version}	" + "</if>"+
    		"</where>"+"</script>")
    List<GrouponCatalogs> selectGrouponCatalogs(Map map);
    
    
    
    /**
     * @param map productId=productId Version=version
	 * status=0：正常 status=1：停止拼团
     * @return
     */
    @Update("<script>update GrouponProducts "
    		+ "set Status=#{status} "
    		+ "<where>"
    		+ "<if test='productId !=null '> ProductId=#{productId}" + "</if>"
    		+ "<if test='version !=null '>  and version=#{version}" + "</if>"
    		+ "<if test='startTime !=null '>  and startTime=#{startTime}" + "</if>"
    		+ "<if test='endTime !=null '>  and endTime=#{endTime}" + "</if>"
    		+ "</where> </script>")
    List<GrouponCatalogs> updateStatusGrouponProducts(Map map);

    @Select("select * from BusinessCredentialInfo where ProductId = #{productId} and ActionType = #{actionType} and ExtParam = #{ExtParam}")
    List<Map> selectBusinessCredentialInfo(Map map);

    @Select("select * from ymt_AgentProducts where ProductId = #{productId}")
    List<AgentProductPo> selectAgentProductByProductId(Map map);

    @Select("select * from ymt_AgentProducts where ProductId = #{productId} and UserId=#{userid}")
    List<AgentProductPo> selectAgentProductByProductIdAndUserId(Map map);

    @Select("select * from ymt_AgentProducts where ParentProductId = #{parentProductId}")
    List<AgentProductPo> selectAgentProductByParentProductId(Map map);
    @Select("select * from ymt_AgentProducts where ParentProductId = #{parentProductId} and UserId=#{userid}")
    List<AgentProductPo> selectAgentProductByParentProductIdAndUserId(Map map);
    @Select("select * from ymt_AgentPrice where CatalogId = #{catalogId} and AgentTypeId = #{agnetTypeId}")
    List<AgentPricePo> selectAgentPriceByCatalogId(Map map);

    @Select("select * from ymt_AgentPrice where CatalogId = #{catalogId}")
    List<AgentPricePo> selectAgentPriceById(Map map);

    @Select("select * from ymt_AgentPrice where ProductId = #{producId} and UserId = #{userId}")
    List<AgentPricePo> selectAgentPriceByCatalogIdAndUserId(Map map);

    @Select("select * from ymt_AgentPrice where CatalogId = #{catalogId} and AgentTypeId = #{agentTypeId}")
    List<AgentPricePo> selectAgentPriceByCatalogIdAndAgentType(Map map);

    @Select("select * from ymt_AgentProducts where UserId = #{userId} and Action = #{action}")
    List<AgentProductPo> selectAgentProductByUserId(Map map);

    @Update("update ymt_AgentProducts set InfoProgress = #{infoProgress} where ProductId = #{ProductId}")
    int updateAgentProductInfoProgress(Map m);

    @Select("select * from ymt_AgentProducts where RootProductId = #{productId}")
    List<AgentProductPo> selectAgentProductByRootProductId(Map map);

    @Select("SELECT * FROM Ymt_ProductCategory where iCategoryId = #{categoryId}")
    List<Map> selectProductCategoryByCategoryId(Map m);

    @Select("SELECT * FROM Ymt_ProductCategory where iCategoryId = #{categoryId}")
    List<Category> selectProductCategoryByCategoryIdV2(Map m);

    @Select("select * from ymt_AgentPrice where AgentTypeId = #{agentTypeId}")
    List<AgentPricePo> selectAgentPriceByAgentType(Map m);

    @Select("select t.iCategoryId as thirdCategoryId,t.sCategory as thirdCategoryName," +
            "s.iCategoryId as secondCategoryId,s.sCategory as secondCategoryName,f.iCategoryId " +
            "as firstCategoryId,f.sCategory as firstCategoryName from Ymt_ProductCategory t " +
            "LEFT JOIN Ymt_ProductCategory s on s.iCategoryId = t.iMasterCategory and s.iMasterCategory is not null " +
            "LEFT JOIN Ymt_ProductCategory f on f.iCategoryId = s.iMasterCategory and f.iMasterCategory is not null " +
            "where t.iCategoryId = #{thirdCategoryId}")
    List<CategoryDtoV2> selectCategoryByThirdCategoryId(Map m);

    @Select("SELECT * FROM Ymt_AgentWxCodePic where ProductId = #{productId}")
    List<Map> selectAgentWxCodePicByProductId(Map m);

    List<PriceModel> selectAgentProductPrice(Map map);

    @Select("select * from Ymt_ProductPicture where iAction > -1 AND sProductId = #{productId}")
    List<Map> selectProductPictures(Map m);

    List<Map> selectUserCatalogProperty(Map m);

    @Select("select * from Ymt_UserCatalogProperty where UserId = #{sellerId} or UserId = 0")
    List<Map> selectUserCatalogPropertyByUserId(Map m);
}